package com.openbravo.pos.printer.javapos;

import com.openbravo.data.loader.ImageUtils;
import com.openbravo.pos.printer.DevicePrinter;
import com.openbravo.pos.printer.TicketPrinterException;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.swing.JComponent;
import jpos.CashDrawer;
import jpos.JposException;
import jpos.POSPrinter;

public class DevicePrinterJavaPOS
  implements DevicePrinter
{
  private static final String JPOS_SIZE0 = "\033|1C";
  private static final String JPOS_SIZE1 = "\033|2C";
  private static final String JPOS_SIZE2 = "\033|3C";
  private static final String JPOS_SIZE3 = "\033|4C";
  private static final String JPOS_LF = "\n";
  private static final String JPOS_BOLD = "\033|bC";
  private static final String JPOS_UNDERLINE = "\033|uC";
  private static final String JPOS_CUT = "\033|100fP";
  private String m_sName;
  private POSPrinter m_printer = null;
  private CashDrawer m_drawer = null;
  private StringBuffer m_sline;

  public DevicePrinterJavaPOS(String paramString1, String paramString2)
    throws TicketPrinterException
  {
    this.m_sName = paramString1;
    if ((paramString2 != null) && (!paramString2.equals("")))
      this.m_sName = (this.m_sName + " - " + paramString2);
    try
    {
      this.m_printer = new POSPrinter();
      this.m_printer.open(paramString1);
      this.m_printer.claim(10000);
      this.m_printer.setDeviceEnabled(true);
      this.m_printer.setMapMode(4);
    }
    catch (JposException localJposException1)
    {
      throw new TicketPrinterException(localJposException1.getMessage(), localJposException1);
    }
    try
    {
      this.m_drawer = new CashDrawer();
      this.m_drawer.open(paramString2);
      this.m_drawer.claim(10000);
      this.m_drawer.setDeviceEnabled(true);
    }
    catch (JposException localJposException2)
    {
      this.m_drawer = null;
    }
  }

  public String getPrinterName()
  {
    return this.m_sName;
  }

  public String getPrinterDescription()
  {
    return null;
  }

  public JComponent getPrinterComponent()
  {
    return null;
  }

  public void reset()
  {
  }

  public void beginReceipt()
  {
    try
    {
      this.m_printer.transactionPrint(2, 11);
    }
    catch (JposException localJposException)
    {
    }
  }

  public void printImage(BufferedImage paramBufferedImage)
  {
    try
    {
      if (this.m_printer.getCapRecBitmap())
      {
        File localFile = File.createTempFile("jposimg", ".png");
        FileOutputStream localFileOutputStream = new FileOutputStream(localFile);
        localFileOutputStream.write(ImageUtils.writeImage(paramBufferedImage));
        localFileOutputStream.close();
        this.m_printer.printBitmap(2, localFile.getAbsolutePath(), -11, -2);
      }
    }
    catch (IOException localIOException)
    {
    }
    catch (JposException localJposException)
    {
    }
  }

  public void printBarCode(String paramString1, String paramString2, String paramString3)
  {
    try
    {
      if (this.m_printer.getCapRecBarCode())
        if ("none".equals(paramString2))
          this.m_printer.printBarCode(2, paramString3, 104, 1000, 6000, -2, -11);
        else
          this.m_printer.printBarCode(2, paramString3, 104, 1000, 6000, -2, -13);
    }
    catch (JposException localJposException)
    {
    }
  }

  public void beginLine(int paramInt)
  {
    this.m_sline = new StringBuffer();
    if (paramInt == 0)
      this.m_sline.append("\033|1C");
    else if (paramInt == 1)
      this.m_sline.append("\033|2C");
    else if (paramInt == 2)
      this.m_sline.append("\033|3C");
    else if (paramInt == 3)
      this.m_sline.append("\033|4C");
    else
      this.m_sline.append("\033|1C");
  }

  public void printText(int paramInt, String paramString)
  {
    if ((paramInt & 0x1) != 0)
      this.m_sline.append("\033|bC");
    if ((paramInt & 0x2) != 0)
      this.m_sline.append("\033|uC");
    this.m_sline.append(paramString);
  }

  public void endLine()
  {
    this.m_sline.append("\n");
    try
    {
      this.m_printer.printNormal(2, this.m_sline.toString());
    }
    catch (JposException localJposException)
    {
    }
    this.m_sline = null;
  }

  public void endReceipt()
  {
    try
    {
      this.m_printer.printNormal(2, "\033|100fP");
      this.m_printer.transactionPrint(2, 12);
    }
    catch (JposException localJposException)
    {
    }
  }

  public void openDrawer()
  {
    if (this.m_drawer != null)
      try
      {
        this.m_drawer.openDrawer();
      }
      catch (JposException localJposException)
      {
      }
  }

  public void finalize()
    throws Throwable
  {
    this.m_printer.setDeviceEnabled(false);
    this.m_printer.release();
    this.m_printer.close();
    if (this.m_drawer != null)
    {
      this.m_drawer.setDeviceEnabled(false);
      this.m_drawer.release();
      this.m_drawer.close();
    }
    super.finalize();
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.pos.printer.javapos.DevicePrinterJavaPOS
 * JD-Core Version:    0.6.0
 */